package com.zcsy.manage.web.base;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zcsy.commons.constants.OwnerConstants;
import com.zcsy.commons.model.RequestConstants;
import com.zcsy.commons.model.RequestJson;
import com.zcsy.commons.unique.UniqueNumberUtils;
import com.zcsy.commons.util.DateUtils;
import com.zcsy.manage.model.base.CommonToken;
import com.zcsy.manage.model.mall.GoodsCollection;
import com.zcsy.manage.model.system.User;
import com.zcsy.manage.service.mall.IGoodsCollectionService;

/**
 * @Title: OwnerOtherController.java
 * @Package： com.zcsy.manage.web.base 
 * @Description: 业主其他控制层
 * @author yangfan 373188993@qq.com
 * @date 2016年8月4日
 * @version 1.0
 * @see JDK1.7.0_75
 * @Copyright: 2016 武汉恒数利通技术有限公司 All rights reserved.
 */

@Controller
@RequestMapping(value = "ownerOther")
public class OwnerOtherController {
    
    @Autowired
    private IGoodsCollectionService goodsCollectionService;
    
    /**
     * @Description:我的收藏列表
     * @author yangfan 373188993@qq.com
     * @date 2016年8月4日
     */
    @RequestMapping(value="getMyCollection" , method = RequestMethod.GET)
    public String getMyCollection(Model model, HttpServletRequest req){
        
        //获取userId
        User user = (User)req.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
        String userID = user.getId();
        
        // 根据业主id联表查询收藏列表
        List<GoodsCollection> goodsCollection = goodsCollectionService.getGoodsCollectionByOwnerId(userID);
        model.addAttribute("record", goodsCollection);
        
        return "base/ownerother/myCollection";
        
    }
    
    /**
     * @Method: collectionAddRemove
     * @Description 商品收藏/取消收藏接口
     * @param GoodsId
     * @param ownerId
     * @return 
     * @author yangfan 373188993@qq.com
     * @date 2016年8月4日
     * @see 
     */
    @RequestMapping(value = "collectionRemove", method = RequestMethod.GET)
    @ResponseBody
    public RequestJson collectionRemove(String goodsId, String ownerId) {

        RequestJson result = new RequestJson();

        // 判断商品是否已经收藏
        GoodsCollection goodsCollection = goodsCollectionService
                .getGoodsCollectionByOwnerIdAndGoodsId(ownerId, goodsId);
        // 已收藏过该商品，操作为取消收藏
        if (goodsCollection != null) {
            try {
                int i = goodsCollectionService.deleteByOwnerIdAndGoodsId(ownerId,
                        goodsId);
                result.setSuccess(true);
            } catch (Exception e) {
                result.setSuccess(false);
            }
        } 

        return result;
    }
}
